package com.ruoyi.web.controller.housingresources;

import com.ruoyi.building.service.IAmBuildingService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.housingresources.domain.AmHousingResources;
import com.ruoyi.housingresources.service.IAmHousingResourcesService;
import com.ruoyi.project.service.IAmProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.ruoyi.common.utils.StringUtils.isEmpty;

/**
 * 房源管理Controller
 * 
 * @author ruoyi
 * @date 2020-07-28
 */
@RestController
@RequestMapping("/housingresources/housingresources")
@Api(description = "房源管理")
public class AmHousingResourcesController extends BaseController
{
    @Autowired
    private IAmHousingResourcesService amHousingResourcesService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IAmProjectService projectService ;

    @Autowired
    private IAmBuildingService buildingService;

    /**
     * 查询房源管理列表
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:list')")
    @GetMapping("/list")
    @ApiOperation("查询房源管理列表")
    public TableDataInfo list(AmHousingResources amHousingResources)
    {
        startPage();
        List<AmHousingResources> list = amHousingResourcesService.selectAmHousingResourcesList(amHousingResources);
        return getDataTable(list);
    }


    /**
     * 导出房源管理列表
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:export')")
    @Log(title = "房源管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出房源管理列表")
    public AjaxResult export(AmHousingResources amHousingResources)
    {
        List<AmHousingResources> list = amHousingResourcesService.selectAmHousingResourcesList(amHousingResources);
        ExcelUtil<AmHousingResources> util = new ExcelUtil<AmHousingResources>(AmHousingResources.class);
        return util.exportExcel(list, "housingresources");
    }

    @GetMapping("/importTemplate")
    @ApiOperation("导入模板")
    public AjaxResult importTemplate()
    {
        ExcelUtil<AmHousingResources> util = new ExcelUtil<AmHousingResources>(AmHousingResources.class);
        return util.importTemplateExcel("房源数据");
    }


    @Log(title = "楼宇管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:import')")
    @PostMapping("/importData")
    @ApiOperation("导入接口")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        if(null == file || !file.getOriginalFilename().contains("xls")){
            return AjaxResult.error("格式不支持");
        }
        ExcelUtil<AmHousingResources> util = new ExcelUtil<AmHousingResources>(AmHousingResources.class);
        List<AmHousingResources> housingResources = util.importExcel(file.getInputStream());
        if (null == housingResources || housingResources.size() == 0 ){
            return AjaxResult.error("请不要上传空文件");
        }


        List<String> projectList = projectService.selectAllAmProjectList();
        List<String> buildList = buildingService.selectAllAmBuildingList();
        List<String> resourceList = amHousingResourcesService.selectAllAmHousingResourceList();


        //非空判断
        for (int i = 0 ; i< housingResources.size() ; i++){
            AmHousingResources resources = housingResources.get(i);
            if (null == resources){
                return AjaxResult.error("第"+(i+2)+"行数据缺失或者表格不正确,请检查后在重新导入");
            }else if (isEmpty( resources.getBuildingName()) ) {
                return AjaxResult.error("第"+(i+2)+"行楼宇名称缺失,请检查后在重新导入");
            }else if ( StringUtils.isEmpty( resources.getRoomNum() )){
                return AjaxResult.error("第"+(i+2)+"行房号缺失,请检查后在重新导入");
            }else if ( StringUtils.isEmpty(resources.getArea() )){
                return AjaxResult.error("第"+(i+2)+"行面积缺失,请检查后在重新导入");
            }else if ( StringUtils.isEmpty( resources.getStatus() )){
                return AjaxResult.error("第"+(i+2)+"行房源状态缺失或者不正确,请检查后在重新导入");
            }else if ( StringUtils.isNull( resources.getFloor() )){
                return AjaxResult.error("第"+(i+2)+"行所在楼层缺失,请检查后在重新导入");
            }else if (  StringUtils.isNull(  resources.getPreLeasePrice())){
                return AjaxResult.error("第"+(i+2)+"行预租单价缺失,请检查后在重新导入");
            }else if ( StringUtils.isEmpty(resources.getDecorating())){
                return AjaxResult.error("第"+(i+2)+"行装修状态缺失或者不正确缺失,请检查后在重新导入");
            }else if ( StringUtils.isEmpty( resources.getDescription() )){
                return AjaxResult.error("第"+(i+2)+"行房源描述缺失,请检查后在重新导入");
            }else if (StringUtils.isEmpty(resources.getProjectName())){
                return AjaxResult.error("第"+(i+2)+"行项目名称缺失,请检查后在重新导入");
            }else if(!projectList.contains(resources.getProjectName())){
                return AjaxResult.error("第"+(i+2)+"行项目名称不存在,请检查后在重新导入");
            }else if(!buildList.contains(resources.getProjectName()+resources.getBuildingName())){
                return AjaxResult.error("第"+(i+2)+"行楼宇名称不存在,请检查后在重新导入");
            }else if(resourceList.contains(resources.getProjectName()+resources.getBuildingName()+resources.getRoomNum())){
                return AjaxResult.error("第"+(i+2)+"行房号重复,请检查后在重新导入");
            }
        }
        SysUser user = tokenService.getLoginUser().getUser();
        String message = amHousingResourcesService.importProject(housingResources, updateSupport,user);
        return AjaxResult.success(message);
    }

    /**
     * 获取房源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取房源管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amHousingResourcesService.selectAmHousingResourcesById(id));
    }

    /**
     * 新增房源管理
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:add')")
    @Log(title = "房源管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增房源管理")
    public AjaxResult add(@RequestBody AmHousingResources amHousingResources)
    {
        return toAjax(amHousingResourcesService.insertAmHousingResources(amHousingResources));
    }

    /**
     * 修改房源管理
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:edit')")
    @Log(title = "房源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改房源管理")
    public AjaxResult edit(@RequestBody AmHousingResources amHousingResources)
    {
        return toAjax(amHousingResourcesService.updateAmHousingResources(amHousingResources));
    }

    /**
     * 删除房源管理
     */
    @PreAuthorize("@ss.hasPermi('housingresources:housingresources:remove')")
    @Log(title = "房源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除房源管理")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amHousingResourcesService.deleteAmHousingResourcesByIds(ids));
    }
}
